<template>
  <div class="login">
    <div class="content">
      <h3>平台登录</h3>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item prop="name">
          <el-input v-model="ruleForm.name" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" placeholder="密码" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        name: "",
        password: ""
      },
      rules: {
        name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //表单验证成功，登录
          var str = `username=${this.ruleForm.name}&password=${this.ruleForm.password}`;
          this.axios.post("login", str)
          .then(res => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message({
                message: res.data.meta.msg,
                type: 'success'
              });
              var token=res.data.data.token;
              sessionStorage.setItem("token",res.data.data.token);
              sessionStorage.setItem("uname",res.data.data.username);

              this.$router.push("/");
            }else{
              this.$message.error(res.data.meta.msg);
            }
          })
        } else {
          this.$router.push("/Login");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  height: 69%;
  background-image: url("../assets/map.jpg");
  background-repeat: no;
}

.content {
  background-color: white;
  width: 400px;
  height: 400px;
  position: absolute;
  right: 15%;
  top: 10%;
  padding: 40px;
}



.btn {
  width: 100%
}
</style>
